﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateGbmpPointFamilyInstance.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "StructureCmdParamIds.h"

#include "CommandParameters.h"
#include "IFamily.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "IElementBasicInformation.h"
#include "CmdStatusUtils.h"
#include "GbmpActionCreatePointFamilyInstance.h" 
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateGbmpPointFamilyInstance);

CmdCreateGbmpPointFamilyInstance::CmdCreateGbmpPointFamilyInstance()
    : GbmpCommandBase(ID_CMD_CREATE_GBMP_POINT_FAMILY_INSTANCE)
{
}

CmdCreateGbmpPointFamilyInstance::~CmdCreateGbmpPointFamilyInstance()
{
}

OwnerPtr<IAction> CmdCreateGbmpPointFamilyInstance::ExecuteCommand(const CommandParameters& cmdParams)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(cmdParams.size() == 2, L"命令参数个数不正确",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    // 获取familyName
    auto itor = cmdParams.find(ID_CMD_PARAM_FAMILY_NAME);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(itor != cmdParams.end(), L"所需的参数不存在",L"GDMPLab",L"2024-03-30");
    std::wstring familyName = itor->second.AsWString();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(!familyName.empty(), L"familyName为空",L"GDMPLab",L"2024-03-30");
 
    // 获取familyTypeName，传入的实际是InstanceType
    itor = cmdParams.find(ID_CMD_PARAM_INSTANCE_TYPE);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(itor != cmdParams.end(), L"所需的参数不存在",L"GDMPLab",L"2024-03-30");
    std::wstring instanceType = itor->second.AsWString();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(!instanceType.empty(), L"instanceType为空",L"GDMPLab",L"2024-03-30");
    
    // 获取 pFamily
    IFamily* pFamily = IFamily::GetFamilyByName(pDoc, familyName);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pFamily, L"pFamily为空",L"GDMPLab",L"2024-03-30");

    // 获取ElementId
    ElementId elementId = pFamily->GetBasicInformation()->GetElementId();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(elementId.IsValid(), L"elementId无效",L"GDMPLab",L"2024-03-30");

    return NEW_AS_OWNER_PTR(ActionCreatePointFamilyInstance, EWS_On_Workplane, elementId, instanceType);
}

bool CmdCreateGbmpPointFamilyInstance::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

bool CmdCreateGbmpPointFamilyInstance::IsEnabled() const
{
    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}
